<?php
/* how to...
$api_express=new api_express;
// 参数: 快递公司编号,快递单号
$api_express->get_track('yto','800712813696');

www.ecomgear.cn
*/

class API_express
{
	public $api_id = '108103'; // 
	public $api_key = 'b9fe27741056c62facb1db0130451372'; // 
	
	function __construct(){}
	
	public function get_track($company='',$code=''){
		$url='http://api.ickd.cn/?id='.$this->api_id.'&secret='.$this->api_key.'&com='.$company.'&nu='.$code.'&type=json&encode=utf8&ord=desc&lang=';
		if($company=='' || $code='') return '缺少公司编号或单号';

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER,0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
		curl_setopt($curl, CURLOPT_TIMEOUT,5);
		$result = curl_exec($curl);
		curl_close ($curl);
		$temp=json_decode($result);
		
		$code=$temp->errCode;
		$data=$temp->data;
		$str='';
		switch($code){
			default: $str.='查询服务器故障, 请登录快递公司网站手动查询'; break;
			case 0:
			$str='<table>';
			foreach($data as $val){
				$str.='<tr><td>'.$val->time.'</td><td>'.$val->context.'</td></tr>';
			}
			$str.='</table>';
			break;
			case 1: $str.='单号不存在'; break;
			case 6: $str.='快递单号格式错误'; break;
			case 7: $str.='快递公司错误'; break;

		}
		pre($str);
		exit;
		//return false;
	}
}